package edu.jmu.jdbc;

import static org.junit.Assert.fail;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import com.mysql.jdbc.Statement;

public class JdbcTemplateTest {
	private String xmlPath = "JdbcTemplateBeans.xml";
	private ApplicationContext context = new ClassPathXmlApplicationContext(xmlPath);
	private UserDao userDao = (UserDao)context.getBean("userDao");
	

	@Test
	public void addUserTest() {
		
		User user = new User();
		user.setUsername("zhang");
		user.setPassword("abcdefg");
		int flag = userDao.addUser(user);
		if(flag==1){
			System.out.println("添加用户成功");
		}else{
			System.out.println("添加失败！");
		}
	}
	
	@Test
	public void addUserReturnKey(){
		JdbcTemplate jdbcTemplate = (JdbcTemplate)context.getBean("jdbcTemplate");
		String sql = "insert into t_user(username,password) values(?,?)";
		KeyHolder keyHolder = new GeneratedKeyHolder();
		jdbcTemplate.update(new PreparedStatementCreator() {
			@Override
			public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
				PreparedStatement ps = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
				ps.setString(1, "老王");
				ps.setString(2, "123456");
				return ps;
			}
		},keyHolder);
		System.out.println("插入记录的id为:"+keyHolder.getKey().intValue());
	}
	
	@Test
	public void updateUserTest(){
		User user = new User();
		user.setUserid(1);
		user.setUsername("wang");
		user.setPassword("123");
		int flag = userDao.updateUser(user);
		if(flag==1){
			System.out.println("修改用户成功");
		}else{
			System.out.println("修改失败！");
		}
	}
	
	@Test
	public void deleteUserTest(){
		int flag = userDao.deleteUserById(1);
		if(flag==1){
			System.out.println("删除用户成功");
		}else{
			System.out.println("删除失败！");
		}
	}
	
	@Test
	public void findUserByIdTest(){
		int id = 2;
		User user = userDao.findUserById(id);
		System.out.println(user);
	}
	
	@Test
	public void findUserByIdRowMapperTest(){
		int id = 2;
		User user = userDao.findUserByIdRowMapper(id);
		System.out.println(user);
		
		//findUserByIdCallbackHandler
		
	}
	
	@Test
	public void findUserByIdCallbackHandlerTest(){
		int id = 2;
		User user = userDao.findUserByIdCallbackHandler(id);
		System.out.println(user);
		//findUserByIdCallbackHandler
		
	}
	
	@Test
	public void findAllUserTest(){
		List<User> user = userDao.findAllUser();
		for (User e : user) {
			System.out.println(e);
		}
	}
	
	@Test
	public void findAllUserTestCallbackHanlder(){
		List<User> user = userDao.findAllUserCallbackHandler();
		for (User e : user) {
			System.out.println(e);
		}
	}
	
	@Test
	public void countAllTest(){
		int rowCount  = userDao.countAll();
		System.out.println(rowCount);
	}
	@Test
	public void otherTest(){
		System.out.println("OtherTest begin");
		userDao.otherTest();
		System.out.println("OtherTest end");
	}

}
